﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Auditing;
using Abp.Dependency;
using Abp.Rebus.Auditing;
using Castle.MicroKernel.Registration;
using Rebus.Activation;
using Rebus.CastleWindsor;
using Rebus.Config;
using Rebus.Handlers;
using Rebus.RabbitMq;

namespace Abp.Rebus.Core
{
    public class RebusConfiguration
    {
        public static void Start(string serverUrl)
        {
            var container = IocManager.Instance.IocContainer;
            var adapter = new CastleWindsorContainerAdapter(container);


            var bus = Configure.With(adapter)
                .Transport(t => t.UseRabbitMqAsOneWayClient(serverUrl))
                .Start();
          
        }
        public static void Start(string serverUrl, string queue)
        {
            var container = IocManager.Instance.IocContainer;
            var adapter = new CastleWindsorContainerAdapter(container);


            var bus = Configure.With(adapter)
                .Transport(t => t.UseRabbitMq(serverUrl, queue))
                .Start();
        }


        public static IHandlerActivator GetAbpActivator(FromAssemblyDescriptor descriptor)
        {
            var container = IocManager.Instance.IocContainer;
            var activator = new CastleWindsorContainerAdapter(container);

            container.Register(descriptor
            .BasedOn<IHandleMessages>()
            .WithServiceAllInterfaces()
            .LifestyleTransient());
            return activator;
        }


    }
}
